﻿using System.Drawing;
using Com.OpenCode.Framework.Core;
using DevExpress.XtraBars;

namespace Com.OpenCode.Framework.DevForm
{
    public class RightMenuButtonUI : BarButtonItem, IRightMenuProperty
    {
        private ICommand _Command;

        public RightMenuButtonUI(BuildItemEventArgs args)
        {
            args.Plugin.Owner = this;
            IRightMenuProperty _RightMenuProperty = args.Owner as IRightMenuProperty;
            OwnerRightMenu = _RightMenuProperty.OwnerRightMenu;
            Caption = args.Plugin.Properties.Get("Text").ToString();
            string _ImagePath = args.Plugin.Properties.Get("Image").ToString();
            PaintStyle = BarItemPaintStyle.CaptionGlyph;
            Plugin imagePlugin = args.Plugin.Addin.Plugin.Search("Icon", "Id", args.Plugin.Properties.GetString("Image"));
            if (imagePlugin != null)
            {
                Image image = (Image)args.Plugin.Addin.Bulid(imagePlugin, null);
                LargeGlyph = image;
                Glyph = image;
            }
            ItemClick += RightMenuButtonUI_ItemClick;
            Plugin commandPlugin = args.Plugin.Addin.Plugin.Search("Class", "Id", args.Plugin.Properties.GetString("Image"));
            if (commandPlugin != null)
            {
                _Command = (ICommand)args.Plugin.Addin.Bulid(commandPlugin, null);
            }
            bool _BeginGroup = false;
            bool.TryParse(args.Plugin.Properties.Get("BeginGroup").ToString(), out _BeginGroup);
            _RightMenuProperty.BeginGroup = _BeginGroup;
            _RightMenuProperty.Add(this);
        }

        public override bool Enabled
        {
            get
            {
                if (_Command != null)
                {
                    return _Command.Enable();
                }
                return false;
            }
            set { base.Enabled = value; }
        }

        public OpenCodeRightMenuUI OwnerRightMenu { get; set; }

        public bool BeginGroup { get; set; }

        public void Add(BarItem item)
        {
        }

        private void RightMenuButtonUI_ItemClick(object sender, ItemClickEventArgs e)
        {
            _Command.Run();
        }
    }
}